﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

using Microsoft.Practices.Unity;
namespace Skynet.BusinessLogic.BusinessComponents
{
    [ServiceContract]
    public interface IAutoBusinessComponent<TBusinessEntity>
    {
        [OperationContract]
        void Add(TBusinessEntity item);
        [OperationContract]
        void Add(IEnumerable<TBusinessEntity> items);

        [OperationContract]
        void Update(TBusinessEntity item);
        [OperationContract]
        void Update(IEnumerable<TBusinessEntity> items);

        [OperationContract]
        void Delete(TBusinessEntity item);
        [OperationContract]
        void Delete(IEnumerable<TBusinessEntity> items);

        [OperationContract]
        TBusinessEntity GetOne(object key);
        [OperationContract]
        IEnumerable<TBusinessEntity> GetAll();  
    }
    public class AutoBusinessComponent<TBusinessEntity, TRepository> : IAutoBusinessComponent<TBusinessEntity>        
    {
        [Dependency]
        public TRepository Repository { get; set; }
        public AutoBusinessComponent()
        {
            
        }        
        public virtual void Add(TBusinessEntity item)
        {
            dynamic rep = this.Repository;
            rep.Add(item); 
        }

        public virtual void Update(TBusinessEntity item)
        {
            dynamic rep = this.Repository;
            rep.Update(item);  
        }

        public virtual void Delete(TBusinessEntity item)
        {
            dynamic rep = this.Repository;
            rep.Delete(item);
        }

        public virtual TBusinessEntity GetOne(object key)
        {
            if (key == null) return default(TBusinessEntity);
            dynamic rep = this.Repository;
            return rep.GetOne(key);  
        }

        public virtual IEnumerable<TBusinessEntity> GetAll()
        {
            dynamic rep = this.Repository;
            return rep.GetAll();            
        }        
        public virtual void Add(IEnumerable<TBusinessEntity> items)
        {
            dynamic rep = this.Repository;
            rep.Add(items);     
        }
        public virtual  void Update(IEnumerable<TBusinessEntity> items)
        {
            dynamic rep = this.Repository;
            rep.Update(items);
        }
        public virtual void Delete(IEnumerable<TBusinessEntity> items)
        {
            dynamic rep = this.Repository;
            rep.Delete(items);
        }        
    }
}
